<template>
  <div class="home">
    <el-container style="height:100vh;">
      <el-aside style="width:210px;">
        <el-menu
          :style="{height: conHeight}"
          style="box-sizing: border-box;width:210px;"
          :default-active="activeIndex"
          class="el-menu-vertical-demo"
          background-color="#001529"
          text-color="#999"
          active-text-color="#fff"
          :router="true"
        >
          <h4 class="logo">后台管理</h4>
          <el-submenu :index="''+index" v-for="(item, index) in menus" :key="index">
            <template slot="title">
              <i v-if="item.path === 'users'" class="el-icon-user"></i>
              <i v-if="item.path === 'rights'" class="el-icon-menu"></i>
              <i v-if="item.path === 'goods'" class="el-icon-goods"></i>
              <i v-if="item.path === 'orders'" class="el-icon-s-order"></i>
              <i v-if="item.path === 'reports'" class="el-icon-s-promotion"></i>
              <span slot="title">{{item.authName}}</span>
            </template>
            <el-menu-item
              :index="'/home/'+_item.path"
              v-for="(_item, _index) in item.children"
              :key="_index"
            >{{_item.authName}}</el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <el-container>
        <el-header
          style="border-bottom: 1px solid #e6e6e6;display:flex;align-items:center;justify-content:flex-end;"
        >
          <el-dropdown @command="signOut">
            <span class="el-dropdown-link" style="display:flex;align-items:center;">
              <el-avatar src="https://cube.elemecdn.com/0/88/03b0d39583f48206768a7534e55bcpng.png"></el-avatar>
              <i class="el-icon-arrow-down el-icon--right"></i>
            </span>
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item>退出</el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>
        </el-header>
        <el-main style="background:#f5f7f9;padding:15px;">
          <router-view></router-view>
        </el-main>
      </el-container>
    </el-container>
  </div>
</template>

<script type="text/ecmascript-6">
export default {
  data() {
    return {
      menus: [],
      conHeight: 0
    };
  },
  methods: {
    //加载权限菜单
    loadMenus() {
      this.$http("menus", {}, "get").then(res => {
        this.menus = res.data;
      });
    },
    //退出
    signOut() {
      localStorage.removeItem('token')
      this.$store.commit('setToken','')
      this.$message({
        message: '退出成功',
        type: 'success'
      })
      this.$router.replace({
        name: 'login'
      })
    }
  },
  created() {
    this.conHeight = window.innerHeight + "px";
    this.loadMenus();
    this.$nextTick(() => {
      window.addEventListener("resize", () => {
        this.conHeight = window.innerHeight + "px";
      });
    });
  },
  computed: {
    activeIndex() {
      return "/" + this.$route.matched[0].path.split("/")[1];
    }
  },
  mounted() {}
};
</script>

<style scoped lang="scss">
.logo {
  padding: 20px 0;
  color: #fff;
  text-align: center;
}
</style>
